<!DOCTYPE html>
<html>
<head>
    {include file="_Fragment/meta" /}
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-bar">
    {{#if (d.url != '') { }}
    <a class="pear-btn pear-btn-primary pear-btn-md" lay-event="download"><i class="layui-icon layui-icon-download-circle"></i></a>
    {{# } }}
</script>

<script type="text/html" id="table-type">
    {{#if (d.type == 'SchoolRecordJobQueue') { }}
    <span>轻学堂课程学习明细</span>
    {{# }else if(d.type == 'SchoolUserRecordJobQueue'){ }}
    <span>轻学堂员工学习明细</span>
    {{# }else if(d.type == 'DedaoQuestionRecordJobQueue'){ }}
    <span>得到锦囊学习明细</span>
    {{# }else if(d.type == 'DedaoUserQuestionRecordJobQueue'){ }}
    <span>得到锦囊员工学习明细</span>
    {{# }else if(d.type == 'DedaoRecordJobQueue'){ }}
    <span>得到听书学习明细</span>
    {{# }else if(d.type == 'DedaoUserRecordJobQueue'){ }}
    <span>得到听书员工学习明细</span>
    {{# }else if(d.type == 'LiveRecordJobQueue'){ }}
    <span>直播课学习明细</span>
    {{# }else if(d.type == 'LiveUserRecordJobQueue'){ }}
    <span>直播课员工学习明细</span>
    {{# }else if(d.type == 'MembersTotalJobQueue'){ }}
    <span>员工学习明细</span>
    {{# }else if(d.type == 'ClassesTotalJobQueue'){ }}
    <span>所有班级学习明细</span>
    {{# }else if(d.type == 'ClassesRecordJobQueue'){ }}
    <span>班级学习明细</span>
    {{# }else if(d.type == 'LearningRecordJobQueue'){ }}
    <span>E-learning课程学习明细</span>
    {{# }else if(d.type == 'LearningTotalJobQueue'){ }}
    <span>所有E-learning课程学习明细</span>
    {{# } }}
</script>

<script type="text/html" id="table-state">
    {{#if (d.url == '') { }}
    <span>等待生成</span>
    {{# }else{ }}
    <span>已生成</span>
    {{# } }}
</script>

<script type="text/html" id="create-time">
    {{layui.util.toDateString(d.add_time*1000, 'yyyy-MM-dd HH:mm:ss')}}
</script>
<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['table', 'form', 'jquery','common'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let cols = [
            [{
                type: 'checkbox',
                fixed: 'left',
            },{
                    title: 'ID',
                    field: 'id',
                    align: 'center',
                    fixed: 'left',
                },
                {
                    title: '表格名称',
                    field: 'title',
                    align: 'center',
                    fixed: 'left',
                },
                {
                    title: '类型',
                    field: 'type',
                    align: 'center',
                    templet:'#table-type',
                },
                {
                    title: '状态',
                    field: 'url',
                    align: 'center',
                    templet:'#table-state',
                },
                {
                    title: '创建时间',
                    field: 'add_time',
                    align: 'center',
                    templet: '#create-time'
                },
                {
                    title: '完成时间',
                    field: 'complete_time',
                    align: 'center',
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/Document/load',
            page: {layout:['count', 'prev', 'page', 'next', 'skip']},
            limit: 10,
            cols: cols,
            //skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter']
        });

        table.on('tool(user-table)', function(obj) {
            if (obj.event === 'download') {
                window.download(obj);
            }
        });

        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        window.batchRemove = function(obj) {

            var checkIds = common.checkField(obj,'id');
            console.log(checkIds)
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除选中的表格吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/Document/delete",
                    data:{id:checkIds},
                    dataType: 'json',
                    type: 'post',
                    success: function(result) {
                        layer.close(loading);
                        if (result.status == 0) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                table.reload('user-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.download = function(obj) {
            // layer.confirm('确定要下载该表格吗?', {
            //     icon: 3,
            //     title: '提示'
            // }, function(index) {
            //     layer.close(index);
            //     window.open(obj.data.url)
            // });

            layer.confirm('确定要下载该表格吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                window.open("{:url('export')}?id="+obj.data.id, "_blank");
            });
        }

        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
